/*
 *@TODO 1编辑，删除，勾选，收藏
 *@TODO 2->按规格报价
 *@TODO 2->按数量报价 : 计算增量和增数，后台返回值后更新3个总量，一个行量，保存的旧值（若有）
 *@TODO 3-> 选中，未选中
 *@TODO 全选，组合删除(后台计算)
 *@author blade
 *@date 2014-07-14
 */
$$$ = shopCart = {
	decrease_quantity: function(rec_id) {
		var parm = {
			obj: $('#input_item_' + rec_id),
			orig: function() {
				return parseInt(this.obj.val());
			}
		};
		if (parm.orig() > 1) {
			parm.obj.val(parm.orig() - 1);
			parm.obj.get(0).onblur();
		}
	},
	increase_quantity: function(rec_id) {
		var parm = {
			obj: $('#input_item_' + rec_id),
			orig: function() {
				return parseInt(this.obj.val());
			}
		};
		parm.obj.val(parm.orig() + 1);
		parm.obj.get(0).onblur();
	},
	change_quantity: function(rec_id, store_id, spec_id, input) {
		// 过滤输入数据
		var _val = $(input).val(),
			freg = /^[1-9]\d*$/;
		if (!freg.test(_val)) {
			$(input).val('');
			return;
		}

		var goodsid = $(input).closest('table').attr('class'); //bynum
		var goodsids = $('table.' + goodsid), //bynum
			perprice = $('#perprice_' + rec_id + ' i.price'),
			pertotal = $('#pertotal_' + rec_id),
			ordertotal = $('#ordertotal_' + store_id),
			totalnum = $('#totalnum'),
			totalprice = $('#totalprice');

		var bynum_add = 0.00, //bynum
			changed = parseInt($(input).attr('changed')), //上一次数据
			perprice_v = parseFloat(perprice.html()),
			pertotal_v = parseFloat(pertotal.html().replace(/[¥,]/g, '')),
			ordertotal_v = parseFloat(ordertotal.html().replace(/[¥,]/g, '')),
			totalnum_v = parseInt(totalnum.html()),
			totalprice_v = parseFloat(totalprice.html().replace(/[¥,]/g, ''));
		var addnum = parseInt(_val) - changed;

		// 按数量报价
		if (goodsid != 00) {
			var oldnum = 0,
				newnum = 0,
				oldprice = 0.00,
				newprice = 0.00,
				referprice = 0.00,
				refernum = 0;
			// 计算旧的总量和新的总量，旧价格和新价格初始化
			goodsids.each(function(i) {
				var changeds = parseInt($(this).find('.pernum').find('input').attr('changed'));
				var input_checked = $(this).find('.binding').find('input').attr('checked');
				var nev_v = parseInt($(this).find('.pernum').find('input').val());
				if (input_checked) { //勾选恒成立，已做遮罩
					oldnum += changeds;
					newnum += nev_v;
					newprice = oldprice = parseFloat($(this).find('.perprice').attr('changed'));
				}
			});
			//计算最新的价格
			perprices = goodsids.find('.perprice').children('p');
			perprices.each(function(i) {
				$(this).removeClass('current');
				var min = parseInt($(this).attr('min'));
				var gt = parseInt($(this).attr('gt'));
				if (min) { //起订量必须大于0
					var max = parseInt($(this).attr('max'));
					if (i == 0) {
						referprice = parseFloat($(this).attr('price'));
						refernum = min;
					}
					if (newnum >= min && newnum <= max) {
						newprice = parseFloat($(this).attr('price'));
						$(this).addClass('current');
					}
				} else if (gt && newnum >= parseInt(gt)) {
					newprice = parseFloat($(this).attr('price'));
					$(this).addClass('current');
				} else if (newnum < refernum) {
					newprice = referprice;
				}
			});
			//每次变动的总量
			oldTotal = oldnum * oldprice;
			newTotal = newnum * newprice;
			bynum_add = newTotal - oldTotal;
		}

		$.getJSON(CART_SITE_URL + '/index.php?app=cart&act=Ajaxupdate&spec_id=' + spec_id + '&quantity=' + _val + '&jsoncallback=?', function(result) {
			if (result.done) {
				if (goodsid != 00) { //按数量报价
					$$$.add_calc(addnum, bynum_add, ordertotal_v, totalnum_v, totalprice_v, ordertotal, totalnum, totalprice);
					goodsids.each(function() {
						var nev_v = parseInt($(this).find('.pernum').find('input').val());
						var pertotal_bn = $(this).find('.pertotal').find('i').eq(1);
						pertotal_bn.html(wl.price_format(newprice * nev_v, 2));
					});
					goodsids.each(function(i) {
						$(this).find('.perprice').attr('changed', wl.number_format(newprice, 2));
					});
				} else {
					$$$.quantity_calc(perprice_v, addnum, pertotal_v, ordertotal_v, totalnum_v, totalprice_v, pertotal, ordertotal, totalnum, totalprice);
				}
				$(input).attr('changed', _val);
			} else {
				$(input).val($(input).attr('changed'));
				wl.sConfirm({
					'info': result.msg,
					'cancel': true
				});
			}
		});
	},
	quantity_calc: function(perprice_v, addnum, pertotal_v, ordertotal_v, totalnum_v, totalprice_v, pertotal, ordertotal, totalnum, totalprice) {
		var add_val = perprice_v * addnum;
		pertotal_v += add_val;
		ordertotal_v += add_val;
		totalnum_v += addnum;
		totalprice_v += add_val;
		pertotal_v = parseFloat(pertotal_v.toFixed(2));
		ordertotal_v = parseFloat(ordertotal_v.toFixed(2));
		totalprice_v = parseFloat(totalprice_v.toFixed(2));
		pertotal.html(wl.price_format(pertotal_v, 2));
		ordertotal.html(wl.price_format(ordertotal_v, 2));
		totalnum.html(totalnum_v);
		totalprice.html(wl.price_format(totalprice_v, 2));
	},
	del_calc: function(addnum, pertotal_v, ordertotal_v, totalnum_v, totalprice_v, ordertotal, totalnum, totalprice) {
		ordertotal_v -= pertotal_v;
		totalnum_v -= addnum;
		totalprice_v -= pertotal_v;
		ordertotal_v = parseFloat(ordertotal_v.toFixed(2));
		totalprice_v = parseFloat(totalprice_v.toFixed(2));
		ordertotal.html(wl.price_format(ordertotal_v, 2));
		totalnum.html(totalnum_v);
		totalprice.html(wl.price_format(totalprice_v, 2));
	},
	add_calc: function(addnum, pertotal_v, ordertotal_v, totalnum_v, totalprice_v, ordertotal, totalnum, totalprice) {
		ordertotal_v += pertotal_v;
		totalnum_v += addnum;
		totalprice_v += pertotal_v;
		ordertotal_v = parseFloat(ordertotal_v.toFixed(2));
		totalprice_v = parseFloat(totalprice_v.toFixed(2));
		ordertotal.html(wl.price_format(ordertotal_v, 2));
		totalnum.html(totalnum_v);
		totalprice.html(wl.price_format(totalprice_v, 2));
	},
	'handel': {
		binding: function(o, k, j) {
			var refer = {
				bool: $(o).attr('checked'),
				res: 0,
				res2: 0,
				status: 1,
				totalnum: $('#totalnum'),
				totalprice: $('#totalprice'),
				fdata: { //计算缓存
					'addnum': 0,
					'addpertotal': 0
				}
			};
			if (j === 2) {
				var ta = $('.shop-every').find('input[type="checkbox"]'),
					shades = $('div.shop-shade');
				if (refer.bool) {
					if($(o).attr('id')=='shoptotal'){
						$('#shoptotal02').attr('checked', true);
					}else{
						$('#shoptotal').attr('checked', true);
					}
					ta.each(function() {
						$(this).attr('checked', true);
					});
					refer.status = 1;
					shades.hide();
				} else {
					if($(o).attr('id')=='shoptotal'){
						$('#shoptotal02').attr('checked', false);
					}else{
						$('#shoptotal').attr('checked', false);
					}
					ta.each(function() {
						$(this).attr('checked', false);
					});
					refer.status = 0;
					shades.show();
				}
				$.post('index.php?app=cart&act=Ajaxbinding', {
					'type': 'all',
					'status': refer.status
				}, function(result) {
					if (result.done) {
						for (var i in result.data.ordertotal) {
							$('#ordertotal_' + i).html(wl.price_format(result.data.ordertotal[i], 2));
						}
						refer.totalnum.html(result.data.totalnum);
						refer.totalprice.html(wl.price_format(result.data.totalprice, 2));
					} else {
						wl.sConfirm({
							'info': result.msg,
							'cancel': true
						});
					}
				}, 'json');
			} else if (k === 1) {
				var ts = $(o).closest('.shop-seller').siblings('.shop-detail').find('input[type="checkbox"]'),
					ta = $(o).closest('form').children('.shop-every').find('input[type="checkbox"]'),
					tz = $(o).closest('.shop-every').siblings('.shop-total').find('input[type="checkbox"]'),
					store_id = $(o).attr('store'),
					ordertotal = $('#ordertotal_' + store_id),
					shades = $('div[store="' + store_id + '"]');
				var ordertotal_v = parseFloat(ordertotal.html().replace(/[¥,]/g, '')),
					totalnum_v = parseInt(refer.totalnum.html()),
					totalprice_v = parseFloat(refer.totalprice.html().replace(/[¥,]/g, ''));

				if (refer.bool) {
					ts.each(function() {
						if (!$(this).attr('checked')) {
							var rec_id = $(this).attr('name'),
								input = $('#input_item_' + rec_id),
								pertotal = $('#pertotal_' + rec_id);
							var addnum = parseInt(input.val()),
								addpertotal = parseFloat(pertotal.html().replace(/[¥,]/g, ''));
							refer.fdata.addnum += addnum;
							refer.fdata.addpertotal += addpertotal;
							$(this).attr('checked', true);
						}
					});
					ta.each(function() {
						if ($(this).attr('checked')) {
							refer.res++;
						}
					});
					if (refer.res == ta.length) {
						tz.attr('checked', true);
						$('#shoptotal02').attr('checked', true);
					}
					refer.status = 1;
					shades.hide();
				} else {
					tz.attr('checked', false);
					$('#shoptotal02').attr('checked', false);
					ts.each(function() {
						if ($(this).attr('checked')) {
							var rec_id = $(this).attr('name'),
								input = $('#input_item_' + rec_id),
								pertotal = $('#pertotal_' + rec_id);
							var addnum = parseInt(input.val()),
								addpertotal = parseFloat(pertotal.html().replace(/[¥,]/g, ''));
							refer.fdata.addnum += addnum;
							refer.fdata.addpertotal += addpertotal;
							$(this).attr('checked', false);
						}
					});
					refer.status = 0;
					shades.show();
				}

				$.post('index.php?app=cart&act=Ajaxbinding', {
					'type': 'group',
					'store_id': store_id,
					'status': refer.status
				}, function(result) {
					if (result.done) { //只计算结果
						if (refer.status === 0) {
							refer.fdata.addpertotal = parseFloat(refer.fdata.addpertotal.toFixed(2));
							$$$.del_calc(refer.fdata.addnum, refer.fdata.addpertotal, ordertotal_v, totalnum_v, totalprice_v, ordertotal, refer.totalnum, refer.totalprice);
						} else if (refer.status === 1) {
							refer.fdata.addpertotal = parseFloat(refer.fdata.addpertotal.toFixed(2));
							$$$.add_calc(refer.fdata.addnum, refer.fdata.addpertotal, ordertotal_v, totalnum_v, totalprice_v, ordertotal, refer.totalnum, refer.totalprice);
						}
					} else {
						wl.sConfirm({
							'info': result.msg,
							'cancel': true
						});
					}
				}, 'json');
			} else {
				var ts = $(o).closest('.shop-detail').siblings('.shop-detail').find('input[type="checkbox"]'),
					tt = $(o).closest('.shop-detail').siblings('.shop-seller').find('input[type="checkbox"]'),
					ta = $(o).closest('form').children('.shop-every').find('input[type="checkbox"]'),
					tz = $(o).closest('.shop-every').siblings('.shop-total').find('input[type="checkbox"]'),
					bindshade = $(o).next('div.bindshade'),
					rec_id = $(o).attr('name'),
					store_id = $(o).attr('store'),
					input = $('#input_item_' + rec_id),
					pertotal = $('#pertotal_' + rec_id),
					ordertotal = $('#ordertotal_' + store_id),
					shade = $('div[rec="' + rec_id + '"]');
				var addnum = parseInt(input.val()),
					pertotal_v = parseFloat(pertotal.html().replace(/[¥,]/g, '')),
					ordertotal_v = parseFloat(ordertotal.html().replace(/[¥,]/g, '')),
					totalnum_v = parseInt(refer.totalnum.html()),
					totalprice_v = parseFloat(refer.totalprice.html().replace(/[¥,]/g, ''));
				if (refer.bool) {
					ts.each(function() {
						if ($(this).attr('checked')) {
							refer.res++;
						}
					});
					if (refer.res === ts.length) {
						tt.attr('checked', true);
					}
					ta.each(function() {
						if ($(this).attr('checked')) {
							refer.res2++;
						}
					});
					if (refer.res2 == ta.length) {
						tz.attr('checked', true);
						$('#shoptotal02').attr('checked', true);
					}
					refer.status = 1;
					shade.hide();
				} else {
					tt.attr('checked', false);
					tz.attr('checked', false);
					$('#shoptotal02').attr('checked', false);
					refer.status = 0;
					shade.show();
				}

				//按数量报价
				var goodsid = input.closest('table').attr('class'); //bynum
				var goodsids = $('table.' + goodsid); //bynum
				var bynum_add = 0.00; //bynum
				if (goodsid != 00) {
					var oldnum = 0,
						newnum = 0,
						oldprice = 0.00,
						newprice = 0.00,
						referprice = 0.00,
						refernum = 0;
					// 计算旧的总量和新的总量，旧价格和新价格初始化
					goodsids.each(function(i) {
						var input_val = parseInt($(this).find('.pernum').find('input').val());
						var input_checked = $(this).find('.binding').find('input').attr('checked'); //只计算选中的兄弟
						if (input_checked) { //若只有一行时，新旧价格容易乱
							oldnum += input_val;
							newnum += input_val;
						}
						newprice = parseFloat($(this).find('.perprice').attr('changed'));
						oldprice = parseFloat($(this).find('.perprice').attr('changed'));
					});
					//去掉勾的时候新数量减少，恢复时增加上来  ---勾选特有
					if (!refer.bool) {
						oldnum += addnum;
					} else {
						oldnum -= addnum;
					}
					//计算最新的价格
					perprices = goodsids.find('.perprice').children('p');
					perprices.each(function(i) {
						$(this).removeClass('current');
						var min = parseInt($(this).attr('min'));
						var gt = parseInt($(this).attr('gt'));
						if (min) { //起订量必须大于0
							var max = parseInt($(this).attr('max'));
							if (i == 0) {
								referprice = parseFloat($(this).attr('price'));
								refernum = min;
							}
							if (newnum >= min && newnum <= max) {
								newprice = parseFloat($(this).attr('price'));
								$(this).addClass('current');
							}
						} else if (gt && newnum >= parseInt(gt)) {
							newprice = parseFloat($(this).attr('price'));
							$(this).addClass('current');
						} else if (newnum < refernum) {
							newprice = referprice;
						}
					});
					// console.log(oldnum,newnum,newprice,oldprice);
					//按数量报价  去掉勾选数量减少，加上勾选数量增加
					oldTotal = oldnum * oldprice;
					newTotal = newnum * newprice;
					bynum_add = oldTotal - newTotal;
					if (!refer.bool) {
						bynum_add = oldTotal - newTotal;
					} else {
						bynum_add = newTotal - oldTotal;
					}
				}
				//点击遮住，1s后再开放点击
				// bindshade.show();
				// setTimeout(function(){
				// 	bindshade.hide();
				// },1000);
				// console.log(bynum_add,newTotal,oldTotal,newprice);
				// console.log(bynum_add);
				$.post('index.php?app=cart&act=Ajaxbinding', {
					'type': 'single',
					'store_id': store_id,
					'rec_id': rec_id,
					'status': refer.status
				}, function(result) {
					if (result.done) {
						if (refer.status === 0) {
							if (goodsid != 00) {
								$$$.del_calc(addnum, bynum_add, ordertotal_v, totalnum_v, totalprice_v, ordertotal, refer.totalnum, refer.totalprice);
								goodsids.each(function() {
									var nev_v = parseInt($(this).find('.pernum').find('input').val());
									var pertotal_bn = $(this).find('.pertotal').find('i').eq(1);
									pertotal_bn.html(wl.price_format(newprice * nev_v, 2));
								});
								goodsids.each(function(i) {
									$(this).find('.perprice').attr('changed', wl.number_format(newprice, 2));
								});
							} else {
								$$$.del_calc(addnum, pertotal_v, ordertotal_v, totalnum_v, totalprice_v, ordertotal, refer.totalnum, refer.totalprice);
							}
						} else if (refer.status === 1) {
							if (goodsid != 00) {
								$$$.add_calc(addnum, bynum_add, ordertotal_v, totalnum_v, totalprice_v, ordertotal, refer.totalnum, refer.totalprice);
								goodsids.each(function() {
									var nev_v = parseInt($(this).find('.pernum').find('input').val());
									var pertotal_bn = $(this).find('.pertotal').find('i').eq(1);
									pertotal_bn.html(wl.price_format(newprice * nev_v, 2));
								});
								goodsids.each(function(i) {
									$(this).find('.perprice').attr('changed', wl.number_format(newprice, 2));
								});
							} else {
								$$$.add_calc(addnum, pertotal_v, ordertotal_v, totalnum_v, totalprice_v, ordertotal, refer.totalnum, refer.totalprice);
							}
						}
					} else {
						wl.sConfirm({
							'info': result.msg,
							'cancel': true
						});
					}
				}, 'json');
			}
		},
		singledel: function(store_id, rec_id) {
			wl.sConfirm({
				'info': '确定删除吗？',
				'success': function() {
					var shop_every = $('#shop_every_' + store_id), //当前店铺
						shopcart = $('#shopcart-confirm'), //整个购物车
						shop_every_num = shopcart.children('div.shop-every').length, //店铺数
						no_shop = $('#no-shop'),
						ordertotal = $('#ordertotal_' + store_id),
						totalnum = $('#totalnum'),
						totalprice = $('#totalprice'),
						go = false;
					var ordertotal_v = parseFloat(ordertotal.html().replace(/[¥,]/g, '')),
						totalnum_v = parseInt(totalnum.html()),
						totalprice_v = parseFloat(totalprice.html().replace(/[¥,]/g, '')),
						rec_data = {
							'type': 'single',
							'contents': rec_id
						};
					var input = $('#input_item_' + rec_id),
						pertotal = $('#pertotal_' + rec_id),
						detail = input.closest('div.shop-detail').find('input[type="checkbox"]'),
						shop_detail = $('#shop_detail_' + rec_id), //行
						shop_detail_num = shop_every.find('div.shop-detail').length; //行数
					// 按规格报价 未勾选的情况下增量为0
					if (detail.attr('checked')) {
						var addnum = parseInt(input.val()),
							pertotal_v = parseFloat(pertotal.html().replace(/[¥,]/g, ''));
					} else {
						var addnum = 0,
							pertotal_v = 0;
					}
					//按数量报价
					var goodsid = input.closest('table').attr('class'); //bynum
					var goodsids = $('table.' + goodsid); //bynum
					var bynum_add = 0.00; //bynum
					if (goodsid != 00) {
						var oldnum = 0,
							newnum = 0,
							oldprice = 0.00,
							newprice = 0.00;
						// 计算旧的总量和新的总量，旧价格和新价格初始化
						goodsids.each(function(i) {
							var input_val = parseInt($(this).find('.pernum').find('input').val());
							var input_checked = $(this).find('.binding').find('input').attr('checked');
							if (input_checked) {
								oldnum += input_val;
								newnum += input_val;
								newprice = parseFloat($(this).find('.perprice').attr('changed')); //待定
							}
							oldprice = parseFloat($(this).find('.perprice').attr('changed'));
						});
						newnum -= addnum;
						//计算最新的价格
						perprices = goodsids.find('.perprice').children('p');
						perprices.each(function() {
							$(this).removeClass('current');
							var min = parseInt($(this).attr('min'));
							var gt = parseInt($(this).attr('gt'));
							if (min) { //起订量必须大于0
								var max = parseInt($(this).attr('max'));
								if (newnum >= min && newnum <= max) {
									newprice = parseFloat($(this).attr('price'));
									$(this).addClass('current');
								}
							} else if (gt && newnum >= parseInt(gt)) {
								newprice = parseFloat($(this).attr('price'));
								$(this).addClass('current');
							}
						});
						//按数量报价  未勾选的情况下增量为0
						if (newnum == oldnum) {
							bynum_add = 0.00;
						} else {
							oldTotal = oldnum * oldprice;
							newTotal = newnum * newprice;
							bynum_add = oldTotal - newTotal;
						}
					}

					var str = JSON.stringify(rec_data);

					$.post('index.php?app=cart&act=Ajaxdel', {
						'rec_data': str
					}, function(result) {
						if (result.done) {
							if (shop_detail_num > 1) {
								if (goodsid != 00) {
									$$$.del_calc(addnum, bynum_add, ordertotal_v, totalnum_v, totalprice_v, ordertotal, totalnum, totalprice);
									goodsids.each(function() {
										var nev_v = parseInt($(this).find('.pernum').find('input').val());
										var pertotal_bn = $(this).find('.pertotal').find('i').eq(1);
										pertotal_bn.html(wl.price_format(newprice * nev_v, 2));
									});
									goodsids.each(function(i) {
										$(this).find('.perprice').attr('changed', wl.number_format(newprice, 2));
									});
								} else {
									$$$.del_calc(addnum, pertotal_v, ordertotal_v, totalnum_v, totalprice_v, ordertotal, totalnum, totalprice);
								}
								shop_detail.remove();
							} else if (shop_detail_num <= 1 && shop_every_num > 1) {
								if (goodsid != 00) {
									$$$.del_calc(addnum, bynum_add, ordertotal_v, totalnum_v, totalprice_v, ordertotal, totalnum, totalprice);
									goodsids.each(function(i) {
										$(this).find('.perprice').attr('changed', wl.number_format(newprice, 2));
									});
								} else {
									$$$.del_calc(addnum, pertotal_v, ordertotal_v, totalnum_v, totalprice_v, ordertotal, totalnum, totalprice);
								}
								shop_every.remove();
							} else if (shop_detail_num <= 1 && shop_every_num <= 1) {
								shopcart.remove();
								no_shop.removeClass('f-dn');
							}
						} else {
							wl.sConfirm({
								'info': result.msg,
								'cancel': true
							});
						}

					}, 'json');

				}
			});
		},
		groupdel: function(store_id) {
			wl.sConfirm({
				'info': '确定删除吗？',
				'success': function() {
					var shop_every = $('#shop_every_' + store_id), //当前店铺
						shopcart = $('#shopcart-confirm'), //整个购物车
						shop_every_num = shopcart.children('div.shop-every').length, //店铺数
						no_shop = $('#no-shop'),
						ordertotal = $('#ordertotal_' + store_id),
						totalnum = $('#totalnum'),
						totalprice = $('#totalprice');

					var ordertotal_v = parseFloat(ordertotal.html().replace(/[¥,]/g, '')),
						totalnum_v = parseInt(totalnum.html()),
						totalprice_v = parseFloat(totalprice.html().replace(/[¥,]/g, ''));

					var shop_every_inputs = shop_every.children('div.shop-each').children('div.shop-detail').find('input[type="checkbox"]'), //内容所有单选框
						shop_seller_input = shop_every.children('div.shop-each').children('div.shop-seller').find('input[type="checkbox"]'),
						allnum = shop_every_inputs.length,
						checkedinputs = {},
						select = 1,
						checkednum = 0,
						fdata = { //计算缓存
							'addnum': 0,
							'addpertotal': 0
						};
					if (shop_seller_input.attr('checked')) {
						select = 1;
					} else {
						select = 0;
					}
					var rec_data = {
						'type': 'group',
						'store_id': store_id,
						'select': select,
						'contents': {}
					};

					shop_every_inputs.each(function(index) {
						var rec_id = $(this).attr('name'),
							input = $('#input_item_' + rec_id),
							pertotal = $('#pertotal_' + rec_id);
						var addnum = parseInt(input.val()),
							addpertotal = parseFloat(pertotal.html().replace(/[¥,]/g, ''));
						if ($(this).attr('checked')) {
							rec_data.contents[index] = rec_id;
							checkedinputs[index] = $('#shop_detail_' + rec_id);
							checkednum++;
							fdata.addnum += addnum;
						}
					});
					fdata.addpertotal = ordertotal_v;

					var str = JSON.stringify(rec_data);

					$.post('index.php?app=cart&act=Ajaxdel', {
						'rec_data': str
					}, function(result) {
						if (result.done) {
							if (checkednum == 0) {
								alert('请选择删除项');
							} else if (checkednum < allnum) {
								$$$.del_calc(fdata.addnum, fdata.addpertotal, ordertotal_v, totalnum_v, totalprice_v, ordertotal, totalnum, totalprice);
								shop_seller_input.attr('checked', false);
								for (var i in checkedinputs) {
									checkedinputs[i].remove();
								}
								for (var i in result.newprice) {
									var this_newprice = parseFloat(result.newprice[i]);
									var _table = $('table.' + i);
									_table.each(function() {
										var _perprices = $(this).find('.perprice').children('p');
										var this_num = parseInt($(this).find('.pernum').find('input').val());
										var this_pertotal = $(this).find('.pertotal').find('i').eq(1);
										$(this).find('.perprice').attr('changed', this_newprice);
										this_pertotal.html(wl.price_format(this_num * this_newprice, 2)); //金额格式化
										_perprices.each(function() {
											$(this).removeClass('current');
											var this_v = $(this).attr('price');
											if (this_v == this_newprice) {
												$(this).addClass('current');
											}
										});
									});
								}
							} else if (checkednum == allnum && shop_every_num > 1) {
								$$$.del_calc(fdata.addnum, fdata.addpertotal, ordertotal_v, totalnum_v, totalprice_v, ordertotal, totalnum, totalprice);
								shop_every.remove();
							} else if (checkednum == allnum && shop_every_num <= 1) {
								shopcart.remove();
								no_shop.removeClass('f-dn');
							}
						} else {
							wl.sConfirm({
								'info': result.msg,
								'cancel': true
							});
						}
					}, 'json');

				}
			});
		},
		alldel: function() {
			var shoptotal = $('#shoptotal');
			if (!shoptotal.attr('checked')) {
				wl.sConfirm({
					'info': '请全选后再操作',
					'cancel': true
				});
				return;
			}
			wl.sConfirm({
				'info': '确定删除吗？',
				'success': function() {
					var shopcart = $('#shopcart-confirm'), //整个购物车
						no_shop = $('#no-shop');
					var rec_data = {
						'type': 'all',
						'contents': {}
					};
					var str = JSON.stringify(rec_data);
					$.post('index.php?app=cart&act=Ajaxdel', {
						'rec_data': str
					}, function(result) {
						if (result.done) {
							shopcart.remove();
							no_shop.removeClass('f-dn');
						} else {
							wl.sConfirm({
								'info': result.msg,
								'cancel': true
							});
						}
					}, 'json');
				}
			});
		},
		singlefavorite: function(goods_id) {
			$.getJSON(USER_SITE_URL + '/index.php?app=my_favorite&act=add&type=goods&ajax=1&item_id=' + goods_id + '&jsoncallback=?', function(result) {
				if (result.done) {
					wl.sConfirm({
						'info': result.msg,
						'cancel': true
					});
				} else {
					wl.sConfirm({
						'info': result.msg,
						'cancel': true
					});
				}
			});
		},
		allfavorite: function(type) {
			var input = $('#shoptotal'),
				info = '全部收藏成功';
			if (input.attr('checked')) {
				$.getJSON(CART_SITE_URL + '/index.php?app=cart&act=Ajaxfavorite&type=' + type + '&jsoncallback=?', function(result) {
					if (result.done) {
						wl.sConfirm({
							'info': info,
							'cancel': true
						});
					} else {
						wl.sConfirm({
							'info': result.msg,
							'cancel': true
						});
					}
				});
			} else {
				wl.sConfirm({
					'info': '请全选后再操作',
					'cancel': true
				});
			}
		}

	},
	'constant': {
		CHANGED: 0 //购物车 input-changed
	}

};

$(function() {
	$('#shopcar_submit').bind('click', function() {
		var goods_id_data = {
				'goods_id': {},
				'spec_id': {}
			},
			goods_id_str = '',
			shop_detail = $('div.shop-detail');
		var href = $(this).attr('href');
		var count = 0;
		shop_detail.each(function(i) {
			var checked_input = $(this).find('input[type="checkbox"]');
			if (checked_input.attr('checked')) {
				var goodId = $(this).attr('goods_id');
				var specId = $(this).attr('spec_id');
				var spec_val = $(this).find('input.quantity').val();
				var goodId_val = 0;
				goods_id_data.spec_id[specId] = spec_val;
				shop_detail.each(function() {
					var checked_input_two = $(this).find('input[type="checkbox"]');
					if (checked_input_two.attr('checked')) {
						if ($(this).attr('goods_id') == goodId) {
							goodId_val += parseInt($(this).find('input.quantity').val());
						}
					}

				});
				goods_id_data.goods_id[goodId] = goodId_val;
			}
		});

		for (var i in goods_id_data.goods_id) {
			count++;
		}
		for (var j in goods_id_data.spec_id) {
			count++;
		}
		if (count <= 0) {
			wl.sConfirm({
				'info':'请选择商品后继续',
				'cancel':true
			});
		}

		goods_id_str = JSON.stringify(goods_id_data); //验证数据

		$.ajax({
			type: "get",
			url: USER_SITE_URL + "/index.php?app=member&act=Ajax_islogin&jsoncallback=?",
			dataType: "jsonp",
			success: function(data) {
				if (data.done) {
					$.ajax({
						type: "POST",
						url: "index.php?app=cart&act=Ajaxcheckstock",
						data: {
							'goods_ids': goods_id_str
						},
						dataType: "json",
						success: function(data) {
							if (data.done) {
								window.location.href = href;
							} else {
								if (data.data.stock.length > 0) {
									var i, lens = data.data.stock.length;
									for (i = 0; i < lens; i++) {
										var spec_id = data.data.stock[i].spec_id;
										$('div[spec_id="' + spec_id + '"]').find('div.shop-return-info').html(data.data.stock[i].info).show();
									}
									setTimeout(function() {
										$('div.shop-return-info').hide();
									}, 6000);
								}
								if (data.data.start_stock.length > 0) {
									var j, length = data.data.start_stock.length;
									for (j = 0; j < length; j++) {
										var goods_id = data.data.start_stock[j].goods_id;
										$('div[goods_id="' + goods_id + '"]').find('div.shop-return-info').html(data.data.start_stock[j].info).show();
									}
									setTimeout(function() {
										$('div.shop-return-info').hide();
									}, 6000);
								}
							}
						}
					});
				} else {
					wl.showLogin();
				}
			}
		});
		return false;
	});
});